//
//  MainView.m
//  BeanSproutEDU
//
//  Created by douya on 15/1/6.
//  Copyright (c) 2015年 douya. All rights reserved.
//

#import "MainView.h"
#import "MainCell.h"
#import "CycleScrollView.h"
#import "CourseShowView.h"
//#import "MJRefresh.h" 
#import <MJRefresh/MJRefresh.h>
#import <UIScrollView+MJRefresh.h>
#import "HttpManager.h"
#import "BoutiqueCourseView.h"//精品课程view
#import "Header.h"
#import "VideoModel.h"
#import <MediaPlayer/MediaPlayer.h>
@interface MainView()
{
    
}

@property(nonatomic,weak)UIScrollView *currentScrollView;
@end
@implementation MainView
static UIView *view1;
static UIView *view2;

-(id)initWithFrame:(CGRect)frame
{
    _isFirstAddHeadview=YES;
    if ([super initWithFrame:frame])
    {
        UIView *bigView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 50+582)];
        [self addSubview:bigView];
       
        UISegmentedControl *segment=[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"精品课程",@"全部课程" ,nil]];
        segment.frame=CGRectMake(SCREENWIDTH/2-100, 10, 200, 30);
        [segment addTarget:self action:@selector(segmentedControlClick:) forControlEvents:UIControlEventValueChanged];
        segment.selectedSegmentIndex=0;
        [bigView addSubview:segment];
        
        
       
        
        
        //view1用来装全部课程的view
        view1=[[UIView alloc] initWithFrame:CGRectMake(0, 50, SCREENWIDTH, 582)];
        view1.tag=101;
        view1.alpha=0.0f;
        [bigView addSubview:view1];
        //view2用来装精品课程的view
        view2=[[UIView alloc] initWithFrame:CGRectMake(0, 50,SCREENWIDTH, SCREENHEIGHT-113)];
        view2.tag=102;
        view2.backgroundColor=[UIColor blackColor];
//        view2.alpha=0.0f;
        [bigView addSubview:view2];
        
        //精品课程view
        _BCView=[[BoutiqueCourseView alloc] initWithFrame:view2.bounds];
        _BCView.tag=1000;
        [view2 addSubview:_BCView];
        
        
        _allCourseTableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT-64-50) style:UITableViewStylePlain];
        _allCourseTableView.separatorStyle=UITableViewCellSeparatorStyleNone;//取消单元格分割线
        _allCourseTableView.delegate=self;
        _allCourseTableView.dataSource=self;
        _allCourseTableView.showsVerticalScrollIndicator=NO;
        [view1 addSubview:_allCourseTableView];
        
        //为滚动的scrollview创建源数据
        NSMutableArray *viewsArray = [[NSMutableArray alloc] initWithCapacity:0];
        
        for (int i = 0; i < 3; i++) {
            UIImageView *imageView = [[UIImageView alloc] init];
            imageView.bounds=CGRectMake(0, 0, SCREENWIDTH, 150);
            imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"bigScrollView%d.jpg",i + 1]];
            [viewsArray addObject:imageView];
        }
        //创建不返回的scrollview
        self.mainScrollView=[[CycleScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 150) animationDuration:2];
        //往scrollview装载图片
        self.mainScrollView.fetchContentViewAtIndex=^UIView *(NSInteger pageIndex)
        {
            return viewsArray[pageIndex];
        };
        //pageControl的个数
        self.mainScrollView.totalPagesCount=^NSInteger(void)
        {
            return 3;
        };
        //当前点了第几个contentView
        self.mainScrollView.TapActionBlock=^(NSInteger pageIndex)
        {
            NSLog(@"当前点击了第%ld个contentView",(long)pageIndex);
        };
        
        [[NSRunLoop currentRunLoop] addTimer:self.mainScrollView.animationTimer forMode:NSRunLoopCommonModes];//让定时器与主线程不冲突
        
        self.currentScrollView=_BCView.tableView;
        //加入了下拉刷新控件
        [self addHeader:self.currentScrollView];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadData:) name:@"reloadData" object:nil];
    }
    return self;
}

-(void)reloadData:(NSNotification *)noti
{
    NSLog(@"全部课程更新数据啦");
    NSDictionary *dict=(NSDictionary *)noti.object;
    self.NewArray=[dict objectForKey:@"New"];//新课程
    self.HotArray=[dict objectForKey:@"Hot"];//热门课程
    [_allCourseTableView reloadData];
}
//下拉刷新控件
-(void)addHeader:(UIScrollView *)scrollView
{
 
    if (_isFirstAddHeadview==NO)
    {
        return;
    }
    [scrollView addHeaderWithTarget:self action:@selector(headerRereshing)];
}


//刷新完毕开始请求数据
-(void)headerRereshing
{
    [HttpManager getMainInfoWithBlock:^(NSMutableArray *array,BOOL isSuccess)
     {
          [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadNewData" object:nil];
         if (isSuccess)
         {
             [self.currentScrollView headerEndRefreshing];
             
         }
         else
         {
             [self.currentScrollView headerEndRefreshing];
         }
     }];
    
}

-(void)segmentedControlClick:(UISegmentedControl *)segment
{
    
    if (segment.selectedSegmentIndex==0)
    {
        view1.alpha=0;
        view2.alpha=1;
        self.currentScrollView=_BCView.tableView;
        
    }
    else
    {
        view1.alpha=1;
        view2.alpha=0;
     
        self.currentScrollView=_allCourseTableView;
        [self addHeader:self.currentScrollView];
        _isFirstAddHeadview=NO;
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 7;
}

-(MainCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID1=@"cell1";
    static NSString *ID2=@"cell2";
    static NSString *ID3=@"cell3";
    static NSString *ID4=@"cell4";
   
    
    MainCell *cell=nil;
    
    
    
    if (indexPath.row==0)//滚动的ScrollView
    {
        cell=[tableView dequeueReusableCellWithIdentifier:ID1];
        if (!cell)
        {
            NSArray *array=[[NSBundle mainBundle] loadNibNamed:@"MainCell" owner:nil options:nil];
            cell=array[0];
            
        }
  
        [cell addSubview:self.mainScrollView];
        
    }
    else if (indexPath.row==1||indexPath.row==5)//热门推荐和新课程推荐
    {
        cell=[tableView dequeueReusableCellWithIdentifier:ID2];
        if (!cell)
        {
            NSArray *array=[[NSBundle mainBundle] loadNibNamed:@"MainCell" owner:nil options:nil];
            cell=array[1];
            
        }
        if (indexPath.row==1)
        {
            cell.cell2_TItleLabel.text=@"热门推荐";
        }
        else
        {
           cell.cell2_TItleLabel.text=@"新课程推荐";
        }
        
        cell.aKeyPlayBtn.tag=indexPath.row;
        [cell.aKeyPlayBtn addTarget:self action:@selector(aKeyPlayBtn:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    else if (indexPath.row==2||indexPath.row==4||indexPath.row==6)//热门推荐、广告、新课程推荐的ScrollView
    {
        cell=[tableView dequeueReusableCellWithIdentifier:ID3];
        if (!cell)
        {
            NSArray *array=[[NSBundle mainBundle] loadNibNamed:@"MainCell" owner:nil options:nil];
            cell=array[2];

            if (indexPath.row==2)
            {
                for (int i=0; i<self.HotArray.count; i++)
                {
                    VideoModel *vModel=self.HotArray[i];
                    CourseShowView *view=[[CourseShowView alloc] initWithFrame:CGRectMake(10+(80+10)*i, 4, 80, 78)];
                    view.showBtn.tag=i;
                    [view addImageView:vModel.video_thumb setTitle:vModel.video_title];
                    [cell.cell3_ScrollView addSubview:view];
                    [view.showBtn addTarget:self action:@selector(courseBtnClick:) forControlEvents:UIControlEventTouchUpInside];
                }

            }
            if (indexPath.row==6)
            {
                for (int i=0; i<self.NewArray.count; i++)
                {
                    VideoModel *vModel=self.NewArray[i];
                    CourseShowView *view=[[CourseShowView alloc] initWithFrame:CGRectMake(10+(80+10)*i, 4, 80, 78)];
                    view.showBtn.tag=i;
                    [view addImageView:vModel.video_thumb setTitle:vModel.video_title];
                    [cell.cell3_ScrollView addSubview:view];
                    [view.showBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
                }

            }
            

           
            
    }
      
        
        cell.cell3_ScrollView.contentSize=CGSizeMake(10+(80+10)*self.NewArray.count, 0);
        cell.cell3_ScrollView.showsHorizontalScrollIndicator=NO;
        
        
    }
    else//类别选择
    {
        cell=[tableView dequeueReusableCellWithIdentifier:ID4];
        if (!cell)
        {
            NSArray *array=[[NSBundle mainBundle] loadNibNamed:@"MainCell" owner:nil options:nil];
            cell=array[3];
          
        }
    }
    
    
    return cell;
}
-(void)courseBtnClick:(UIButton *)btn
{
    VideoModel *vModel=self.HotArray[btn.tag];
    NSURL *videoURL=[NSURL URLWithString:vModel.video_url];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"BCPlay" object:videoURL];
    
    
}
-(void)btnClick:(UIButton *)btn
{
    VideoModel *vModel=self.NewArray[btn.tag];
    NSURL *videoURL=[NSURL URLWithString:vModel.video_url];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"BCPlay" object:videoURL];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0)//滚动播放
    {
        return 150;
    }
    else if (indexPath.row==1||indexPath.row==5)//热门推荐，新课程推荐
    {
        return 44;
    }
    else if (indexPath.row==2||indexPath.row==6)//scrollView
    {
        return 100;
    }
    else if (indexPath.row==3||indexPath.row==4)
    {
        return 0;
    }
    else//广告btn
    {
        return 44;
    }
}

-(void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.separatorInset=UIEdgeInsetsMake(0, 0, 0, 0);
}
-(void)aKeyPlayBtn:(UIButton *)btn
{
    switch (btn.tag)
    {
        case 1://热门推荐的一键播放
            [[NSNotificationCenter defaultCenter] postNotificationName:@"beginPlay" object:self.HotArray];
            break;
        case 5://新课程的一键播放
             [[NSNotificationCenter defaultCenter] postNotificationName:@"beginPlay" object:self.NewArray];
            break;
        default:
            break;
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
